package com.platform.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.platform.annotation.Authentication;
import com.platform.base.PageUtil;
import com.platform.dao.DeptDAO;
import com.platform.dto.Dept;
import com.platform.exception.BizException;
import com.platform.ext.mybatis.annotation.UseDataSource;
import com.platform.service.IDept;

/**
 * 
 * <p> Title:Bmxx 实现</p>
 * <p> Description:  部门信息</p>
 * <p> Copyright: Copyright (c) 2019 </p>
 * <p> Company: </p>
 *
 * @author wys
 * @version 1.0
 */
@Service

public class IDeptImpl  implements IDept  {
	
	@Autowired
    private DeptDAO<Dept> DAO;
        
    public DeptDAO<Dept> getDAO() {
        return DAO;
    }
    
    /** =========================== 数据库操作 ==================================== **/
    /**
     *  
     * selectAll(查询所有)  requiredPre="DEPT_FIND"
     * (这里描述这个方法适用条件 – 可选)
     * @return 
     * @return List
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication()//requiredPre="DEPTMANAGER_FIND"
    @UseDataSource()
    public List<Dept> selectAll(Map<String,Object> map) {
        List<Dept> s = null;
        try {
            s = DAO.selectByMap(map);
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return s;
    }
    
    @Override
    @Authentication()
    @UseDataSource
    public List<Dept> findDeptUpAndDownByCode(String code) {
        return DAO.findDeptUpAndDownByCode(code);
    }
    
    
    /**
     * 
     * selectTotal(查询总条数)
     * (这里描述这个方法适用条件 – 可选)
     * @param map
     * @return 
     * @return Integer
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication(requiredPre="DEPTMANAGER_FIND")
    @UseDataSource
    public Integer selectTotal(Map<String, Object> map) {
        Integer total = 0; 
        try {
            total = DAO.selectByMapCount(map);
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return total;
    }

    /**
     * 
     * selectByPaging(分页查询)
     * (这里描述这个方法适用条件 – 可选)
     * @param pageUtil
     * @return 
     * @return List<Dept>
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication(requiredPre="DEPTMANAGER_FIND")
    @UseDataSource
    public List<Dept> selectByPaging(PageUtil pageUtil) {
        List<Dept> pagingList = null;
        try {
            pagingList = DAO.selectByMapPaging(pageUtil);
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return pagingList;
    }

    /**
     * 
     * delete(删除)
     * (这里描述这个方法适用条件 – 可选)
     * @param dto 
     * @return void
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication(requiredPre="DEPTMANAGER_DELETED")
    @UseDataSource
    public boolean delete(Dept dto) {
        boolean flag = false;
        try {
            if(DAO.deleteByPrimaryKey(dto) > 0)
                flag = true;
            else 
                flag = false;
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return flag;
    }

    /**
     * 
     * update(修改)
     * (这里描述这个方法适用条件 – 可选)
     * @param dto 
     * @return void
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication(requiredPre="DEPTMANAGER_EDIT")
    @UseDataSource
    public boolean update(Dept dto) {
        boolean flag = false;
        try {
            if(DAO.updateBySelective(dto) > 0)
                flag = true;
            else 
                flag = false;
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return flag;
    }

    /**
     * 
     * insert(新增)
     * (这里描述这个方法适用条件 – 可选)
     * @param dto 
     * @return void
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication(requiredPre="DEPTMANAGER_SAVE")
    @UseDataSource
    public boolean insert(Dept dto) {
        boolean flag = false;
        try {
            if(DAO.insert(dto) > 0)
                flag = true;
            else 
                flag = false;
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return flag;
    }

    @Override
    @Authentication(/*requiredPre="DEPTMANAGER_FIND"*/)
    @UseDataSource
    public Dept get(Object code) {
        Dept dto = null;
        try {
            dto = DAO.selectByPrimaryKey(code);
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return dto;
    }

	@Override
	@Authentication(requiredPre="DEPTMANAGER_FIND")
	@UseDataSource
	public String findCJ(Map<String, Object> map) {
		return DAO.findCJ(map);
	}
	
	
	/**
     * @Title: getDeptTree   
     * @Description: 查询部门组织结构树
     * @author: <a href="mailto:15909910367@163.com">吴先生</a>
     * @return      
     * List<Dept>
     */
	@Override
	@Authentication
	@UseDataSource
    public List<Dept> getDeptTree(){
		List<Dept> depts = DAO.findParentDept();
		buildDeptChild(depts);
    	return depts;
    }
	
	private void buildDeptChild(List<Dept> deptArray){
		List<Dept> deptChilds = Lists.newArrayList();
		for(Dept dept : deptArray) {
			List<Dept> deptTemp = DAO.selectByChild(dept.getId());
			if(deptTemp != null && deptTemp.size() > 0) {
				buildDeptChild(deptTemp);
			}else {
				dept.setLeaf(Boolean.TRUE);//这是根节点
				deptChilds.add(dept);
				continue;
			}
			dept.setChildren(deptTemp);
		}
//		deptArray.removeAll(deptChilds);
	}
	
	//组织机构分层处理
	@Authentication
	@UseDataSource
    public List<Dept> getAllOrgs(String areaCode,String keyWord){
		PageUtil pu = new PageUtil(0, 99999);
		Map<String,Object> searchParamsMap = Maps.newHashMap();
		searchParamsMap.put("areaCode", areaCode);
		searchParamsMap.put("keyWord", keyWord);
		pu.setParams(searchParamsMap);
		List<Dept> orgs = DAO.getAllOrgs(pu);
		buildDeptChild(orgs);
    	return orgs;
    }
}